﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Caching;
using System.Web;
using System.Text.RegularExpressions;
using System.Collections;


namespace Caching
{
    public class AspNetCache : ICache
    {

        /// <summary>
        /// 通过索引器方式,根据键获取相应缓存
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <returns></returns>
        public object this[string key]
        {
            get { return HttpRuntime.Cache["key"]; }
        }

        /// <summary>
        /// 添加一个缓存(默认过期时间为5分钟)  
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <param name="value">缓存值</param>
        /// <returns></returns>
        public bool AddObject(string key, string value)
        {
            object obj = HttpRuntime.Cache.Add(key, value, null, DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
            if (obj != null)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 添加一个缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="value">缓存Value</param>
        /// <param name="ExpiresDateTime">缓存过期时间</param>
        /// <returns></returns>
        public bool AddObject(string key, string value, DateTime ExpiresDateTime)
        {

            object obj = HttpRuntime.Cache.Add(key, value, null, ExpiresDateTime, Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
            if (obj != null)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 根据缓存Key移除缓存
        /// </summary>
        /// <param name="key">缓存key</param>
        /// <returns></returns>
        public bool RemoveObject(string key)
        {
            if (HttpRuntime.Cache[key] != null)
            {
                object obj = HttpRuntime.Cache.Remove(key);
                return obj == null ? false : true;
            }
            else
                return false;
        }

        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="key">缓存key</param>
        /// <returns></returns>
        public object GetObject(string key)
        {
            return HttpRuntime.Cache.Get(key);
        }

        /// <summary>
        /// 根据正则表达式获取缓存列表
        /// </summary>
        /// <param name="expression">匹配表达式</param>
        /// <returns>符合表达式的缓存列表</returns>
        public Dictionary<string, object> GetObjectByExpression(string expression)
        {
            IDictionaryEnumerator itemsInCache = HttpRuntime.Cache.GetEnumerator();
            Dictionary<string, object> cachDic = new Dictionary<string, object>();
            MatchCollection col = null;
            while (itemsInCache.MoveNext())
            {
                System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(expression);
                col = regex.Matches(itemsInCache.Key.ToString());
            }
            if (col != null)
            {
                for (int i = 0; i < col.Count; i++)
                {
                    object obj = HttpRuntime.Cache.Get(col[i].Value.Trim());
                    if (obj != null)
                        cachDic.Add(col[i].Value.ToString(), obj);
                }
            }
            return cachDic;
        }

        public void RemoveObjectByExpression(string expression)
        {
            IDictionaryEnumerator itemsInCache = HttpRuntime.Cache.GetEnumerator();
            IList<object> Caches = new List<object>();
            MatchCollection col = null;
            while (itemsInCache.MoveNext())
            {
                System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(expression);
                col = regex.Matches(itemsInCache.Key.ToString());
            }

            if (col != null)
            {
                for (int i = 0; i < col.Count; i++)
                {
                    object obj = HttpRuntime.Cache.Remove(col[i].Value.Trim());
                }
            }
        }
    }
}
